//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelCashRebate.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Multistep;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProduct.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::Core::IVector<Double>^ paymentTimes, Cephei::QL::Math::IMatrix^ amounts, UInt64 numberOfProducts) : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
    CEvolutionDescription^ _Cevolution;
    CMatrix^ _Camounts;
    try
    {
#ifdef HANDLE
        _phMarketModelCashRebate = NULL;
#endif
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        paymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = paymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        _Camounts = safe_cast<CMatrix^> (amounts);
        _Camounts->Lock();
        QuantLib::Matrix& _amounts = static_cast<QuantLib::Matrix&> (_Camounts->GetReference ()); 
        QuantLib::Size _numberOfProducts = (QuantLib::Size)ValueHelper::Convert (numberOfProducts); //d
        _ppMarketModelCashRebate = new boost::shared_ptr<QuantLib::MarketModelCashRebate> (new QuantLib::MarketModelCashRebate ( _evolution,  _paymentTimes,  _amounts,  _numberOfProducts ));
        SetMarketModelMultiProduct (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (paymentTimes != nullptr) paymentTimes->Unlock();    //not optional
        if (_Camounts != nullptr) _Camounts->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (boost::shared_ptr<QuantLib::MarketModelCashRebate>& childNative, Object^ owner) : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelCashRebate = NULL;
#endif
	_ppMarketModelCashRebate = &childNative;
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (QuantLib::MarketModelCashRebate& childNative, Object^ owner) : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelCashRebate = NULL;
#endif
	_ppMarketModelCashRebate = new boost::shared_ptr<QuantLib::MarketModelCashRebate> (&childNative);
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
    _MarketModelCashRebateOwner = owner;
    _MarketModelMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (CMarketModelCashRebate^ copy) : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelCashRebate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelCashRebate = new boost::shared_ptr<QuantLib::MarketModelCashRebate> (copy->GetShared());
        _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (PLATFORM::Type^ t) : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelCashRebate = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelCashRebate::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (QuantLib::Handle<QuantLib::MarketModelCashRebate>& childNative, Object^ owner)  : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
	_phMarketModelCashRebate = &childNative;
	_ppMarketModelCashRebate = &static_cast<boost::shared_ptr<QuantLib::MarketModelCashRebate>>(childNative.currentLink());
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
    _MarketModelCashRebateOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (QuantLib::Handle<QuantLib::MarketModelCashRebate> childNative)  : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
	_phMarketModelCashRebate = &childNative;
	_ppMarketModelCashRebate = &static_cast<boost::shared_ptr<QuantLib::MarketModelCashRebate>>(childNative.currentLink());
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::CMarketModelCashRebate (QuantLib::MarketModelCashRebate childNative)  : CMarketModelMultiProduct(CMarketModelCashRebate::typeid)
{
#ifdef HANDLE
	_phMarketModelCashRebate = NULL;
#endif
	_ppMarketModelCashRebate = new boost::shared_ptr<QuantLib::MarketModelCashRebate> (new QuantLib::MarketModelCashRebate (childNative));
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppMarketModelCashRebate));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::~CMarketModelCashRebate ()
{
    if (_ppMarketModelCashRebate != NULL)
    {
	    delete _ppMarketModelCashRebate;
        _ppMarketModelCashRebate = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::!CMarketModelCashRebate ()
{
    if (_ppMarketModelCashRebate != NULL)
    {
	    delete _ppMarketModelCashRebate;
    }
}
QuantLib::MarketModelCashRebate& Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::GetReference ()
{
    if (_ppMarketModelCashRebate == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelCashRebate;
}
boost::shared_ptr<QuantLib::MarketModelCashRebate>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::GetShared ()
{
    if (_ppMarketModelCashRebate == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelCashRebate;
}
QuantLib::MarketModelCashRebate* Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::GetPointer ()
{
    if (_ppMarketModelCashRebate == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelCashRebate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelCashRebate>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::GetHandle ()
{
	if (_phMarketModelCashRebate == NULL)
	{
		_phMarketModelCashRebate = new Handle<QuantLib::MarketModelCashRebate> (*_ppMarketModelCashRebate);
	}
	return *_phMarketModelCashRebate;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::HasNative () 
{
	return (_ppMarketModelCashRebate != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::Core::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::Core::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        numberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = numberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        cashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = cashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppMarketModelCashRebate)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (numberCashFlowsThisStep != nullptr) numberCashFlowsThisStep->Unlock(); //NO2  
        if (cashFlowsGenerated != nullptr) cashFlowsGenerated->Unlock(); //NO2  
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelCashRebate)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelCashRebate)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelCashRebate)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelCashRebate)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::IMarketModelCashRebate^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelCashRebate)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelCashRebate)->suggestedNumeraires ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Multistep::IMarketModelCashRebate^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMarketModelCashRebate_Factory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::Core::IVector<Double>^ paymentTimes, Cephei::QL::Math::IMatrix^ amounts, UInt64 numberOfProducts)
{
    return REFNEW CMarketModelCashRebate ( evolution,  paymentTimes,  amounts,  numberOfProducts);
}
